package com.ruoyi.volunteer.activity.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.volunteer.field.domain.VoluntaryField;
import com.ruoyi.volunteer.field.service.IVoluntaryFieldService;
import com.ruoyi.volunteer.target.domain.VoluntaryTarget;
import com.ruoyi.volunteer.target.service.IVoluntaryTargetService;
import com.ruoyi.volunteer.venue.domain.VoluntaryVenue;
import com.ruoyi.volunteer.venue.service.IVoluntaryVenueService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.volunteer.activity.domain.VoluntaryActivity;
import com.ruoyi.volunteer.activity.service.IVoluntaryActivityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 活动管理Controller
 * 
 * @author ruoyi
 * @date 2025-01-08
 */
@RestController
@RequestMapping("/volunteer/activity")
public class VoluntaryActivityController extends BaseController
{
    @Autowired
    private IVoluntaryActivityService voluntaryActivityService;
    @Autowired
    private IVoluntaryFieldService voluntaryFieldService;
    @Autowired
    private IVoluntaryTargetService voluntaryTargetService;
    @Autowired
    private IVoluntaryVenueService voluntaryVenueService;

    /**
     * 一次性查询 服务领域. 对象. 场所
     */
    @GetMapping("/serviceList")
    public AjaxResult servicelist() {
        AjaxResult ajaxResult = AjaxResult.success();
        List<VoluntaryField> voluntaryFieldList = voluntaryFieldService.selectVoluntaryFieldList(null);
        ajaxResult.put("fieldList", voluntaryFieldList);

        List<VoluntaryTarget> voluntaryTargetList = voluntaryTargetService.selectVoluntaryTargetList(null);
        ajaxResult.put("targetList", voluntaryTargetList);

        List<VoluntaryVenue> voluntaryVenueList = voluntaryVenueService.selectVoluntaryVenueList(null);
        ajaxResult.put("venueList", voluntaryVenueList);
        return ajaxResult;
    }

    /**
     * 查询活动管理列表
     */

    @GetMapping("/list")
    public TableDataInfo list(VoluntaryActivity voluntaryActivity)
    {
        List<VoluntaryActivity> list = voluntaryActivityService.selectVoluntaryActivityList(voluntaryActivity);
        return getDataTable(list);
    }

    /**
     * 导出活动管理列表
     */
    @PreAuthorize("@ss.hasPermi('volunteer:activity:export')")
    @Log(title = "活动管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VoluntaryActivity voluntaryActivity)
    {
        List<VoluntaryActivity> list = voluntaryActivityService.selectVoluntaryActivityList(voluntaryActivity);
        ExcelUtil<VoluntaryActivity> util = new ExcelUtil<VoluntaryActivity>(VoluntaryActivity.class);
        util.exportExcel(response, list, "活动管理数据");
    }

    /**
     * 获取活动管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('volunteer:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(voluntaryActivityService.selectVoluntaryActivityById(id));
    }

    /**
     * 新增活动管理
     */
    @PreAuthorize("@ss.hasPermi('volunteer:activity:add')")
    @Log(title = "活动管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VoluntaryActivity voluntaryActivity)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        voluntaryActivity.setBegin(new Date());
        voluntaryActivity.setUserId(loginUser.getUserId());
        return toAjax(voluntaryActivityService.insertVoluntaryActivity(voluntaryActivity));
    }

    /**
     * 修改活动管理
     */
    @PreAuthorize("@ss.hasPermi('volunteer:activity:edit')")
    @Log(title = "活动管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VoluntaryActivity voluntaryActivity)
    {
        return toAjax(voluntaryActivityService.updateVoluntaryActivity(voluntaryActivity));
    }

    /**
     * 删除活动管理
     */
    @PreAuthorize("@ss.hasPermi('volunteer:activity:remove')")
    @Log(title = "活动管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(voluntaryActivityService.deleteVoluntaryActivityByIds(ids));
    }
}
